package tree;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description: 617. 合并二叉树
 * User: Li_yizYa
 * Date: 2025/6/5
 * Time: 20:46
 */
public class Solution26 {
    /**
     * 递归
     * @param root1
     * @param root2
     * @return
     */
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 == null) {
            return root2;
        }
        if (root2 == null) {
            return root1;
        }
        root1.val += root2.val;
        root1.left = mergeTrees(root1.left, root2.left);
        root1.right = mergeTrees(root1.right, root2.right);
        return root1;
    }

    /**
     * 非递归（栈）
     * @param root1
     * @param root2
     * @return
     */
    public TreeNode mergeTrees1(TreeNode root1, TreeNode root2) {
        if (root1 == null) {
            return root2;
        }
        if (root2 == null) {
            return root1;
        }
        Stack<TreeNode> stack1 = new Stack<>();
        Stack<TreeNode> stack2 = new Stack<>();
        stack1.push(root1);
        stack2.push(root2);
        while (!stack1.isEmpty() || !stack2.isEmpty()) {
            TreeNode node1 = stack1.pop();
            TreeNode node2 = stack2.pop();
            node1.val += node2.val;
            if (node1.left != null && node2.left != null) {
                stack1.push(node1.left);
                stack2.push(node2.left);
            } else {
                if (node1.left == null) {
                    node1.left = node2.left;
                }
            }
            if (node1.right != null && node2.right != null) {
                stack1.push(node1.right);
                stack2.push(node2.right);
            } else {
                if (node1.right == null) {
                    node1.right = node2.right;
                }
            }

        }
        return root1;
    }

    /**
     * 非递归（队列）
     * @param root1
     * @param root2
     * @return
     */
    public TreeNode mergeTrees3(TreeNode root1, TreeNode root2) {
        if (root1 == null) {
            return root2;
        }
        if (root2 == null) {
            return root1;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root1);
        queue.offer(root2);
        while (!queue.isEmpty()) {
            TreeNode node1 = queue.poll();
            TreeNode node2 = queue.poll();
            node1.val += node2.val;

            if (node1.left != null && node2.left != null) {
                queue.offer(node1.left);
                queue.offer(node2.left);
            } else {
                if (node1.left == null) {
                    node1.left = node2.left;
                }
            }
            if (node1.right != null && node2.right != null) {
                queue.offer(node1.right);
                queue.offer(node2.right);
            } else {
                if (node1.right == null) {
                    node1.right = node2.right;
                }
            }
        }
        return root1;
    }
}
